﻿using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Threading.Tasks;

namespace Apex.ASPNETCore
{
    /// <summary>
    /// 用于与分布式缓存交互的接口
    /// </summary>
    public interface IDistributedCacheHelper
    {
        /// <summary>
        /// 获取与指定缓存键关联的值，如果不存在则创建新值并将其缓存
        /// </summary>
        /// <typeparam name="TResult">缓存值的类型</typeparam>
        /// <param name="cacheKey">标识缓存值的键</param>
        /// <param name="valueFactory">用于创建要缓存的值的函数</param>
        /// <param name="expireSeconds">缓存值的过期时间（秒）。默认为60秒</param>
        /// <returns>与指定键关联的缓存值</returns>
        TResult? GetOrCreate<TResult>(string cacheKey, Func<DistributedCacheEntryOptions, TResult?> valueFactory, int expireSeconds = 60);

        /// <summary>
        /// 异步获取与指定缓存键关联的值，如果不存在则创建新值并将其缓存
        /// </summary>
        /// <typeparam name="TResult">缓存值的类型</typeparam>
        /// <param name="cacheKey">标识缓存值的键</param>
        /// <param name="valueFactory">用于异步创建要缓存的值的函数</param>
        /// <param name="expireSeconds">缓存值的过期时间（秒）。默认为60秒</param>
        /// <returns>表示异步操作的任务。任务结果包含与指定键关联的缓存值</returns>
        Task<TResult?> GetOrCreateAsync<TResult>(string cacheKey, Func<DistributedCacheEntryOptions, Task<TResult?>> valueFactory, int expireSeconds = 60);

        /// <summary>
        /// 移除与指定缓存键关联的值
        /// </summary>
        /// <param name="cacheKey">要移除的缓存值的键</param>
        void Remove(string cacheKey);

        /// <summary>
        /// 异步移除与指定缓存键关联的值
        /// </summary>
        /// <param name="cacheKey">要移除的缓存值的键</param>
        /// <returns>表示异步操作的任务。</returns>
        Task RemoveAsync(string cacheKey);
    }
}
